package com.alimama.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.alimama.commoms.ResultCode;
import com.alimama.commoms.ResultData;
import com.alimama.shiro.ShiroUtils;
import com.alimama.shop.dto.CommonMsg;
import com.alimama.shop.service.ICustService;
import com.alimama.sms.SmsType;
import com.alimama.utils.CommonCache;
import com.alimama.utils.MD5Utils;
import com.alimama.utils.SmsUtils;
import com.alimama.utils.StringUtils;

@Controller
@RequestMapping("cust")
public class CustController {

	private static final Logger log = LoggerFactory
			.getLogger(CustController.class);

	@Resource
	ICustService custService;

	/**
	 * 登陆
	 * 
	 * @param userName
	 * @param password
	 * @return
	 */
	@RequestMapping("login.action")
	@ResponseBody
	public ResultData loginAction(String param, HttpSession session) {

		ResultData resultData = new ResultData();

		JSONObject jsonObject = JSONObject.parseObject(param);
		String custMbl = jsonObject.getString("username");
		String password = jsonObject.getString("password");
		String smsCode = jsonObject.getString("smsCode");

		if (!StringUtils.verifyNull(password)
				&& !StringUtils.verifyNull(password)) {
			resultData.parse(ResultCode.PARAMS_NOT_NULL);
			return resultData;
		}

		// 动态登陆
		if (StringUtils.verifyNull(smsCode)) {
			String contentKey = SmsUtils.getKey(SmsType.LOGIN_SMS, custMbl,
					"business");
			String smsCodeContent = CommonCache.get(contentKey) == null ? ""
					: (String) CommonCache.get(contentKey);
			if (!smsCode.equals(smsCodeContent)) {
				resultData.parse(ResultCode.CODE_ERROR);
				return resultData;
			}
			CommonCache.delete(contentKey);
		}

		// TODO 邮箱功能暂未完成

		try {

			ShiroUtils.shiroLogin(custMbl, MD5Utils.md5Encode(password));

		} catch (UnknownAccountException ex) {
			resultData.parse(ResultCode.USER_ALREADY_NOT_EXISTS);
		} catch (IncorrectCredentialsException ex) {
			resultData.parse(ResultCode.PASSWORD_ERROR);
		} catch (AuthenticationException ex) {
			resultData.parse(ResultCode.NOT_LOGIN);
		} catch (Exception e) {
			log.error("loginAction.happen.error:", e);
			resultData.parse(ResultCode.SYSTEM_ERROR);
		}
		return resultData;
	}

	/**
	 * 注册
	 * 
	 * @param cust
	 * @return
	 */
	@RequestMapping(value = "register.action", produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public ResultData registerAction(String param) {

		ResultData resultData = new ResultData();
		try {

			JSONObject jsonStr = JSONObject.parseObject(param);
			String custMblOremail = jsonStr.getString("custMblOrEmail");// 手机号或者邮箱
			String password = jsonStr.getString("password");// 密码
			String repeatPassword = jsonStr.getString("repeatPassword");// 重复密码
			String verifyCode = jsonStr.getString("verifyCode");// 短信或邮箱验证码

			// 判断注册信息是否为空
			if (!StringUtils.verifyNull(custMblOremail, password,
					repeatPassword, verifyCode)) {
				resultData.parse(ResultCode.PARAMS_NOT_NULL);
				return resultData;
			}

			// 判断密码是否一致
			if (!password.equals(repeatPassword)) {
				resultData.parse(ResultCode.PASSWORD_NOT_SAMPLE);
				return resultData;
			}

			//如果传入用户名中包含@符号，那么用户肯定是用邮箱注册
			if(custMblOremail.indexOf("@")>-1){
				// 验证邮箱 TODO
				if (!StringUtils.verifyEmail(custMblOremail)) {
					resultData.parse(ResultCode.NO_SUPPORT_EMAIL);
					return resultData;
				}
			}else{
				
			}

			// 验证手机格式
			if (!StringUtils.verifyPhone(custMblOremail)) {
				resultData.parse(ResultCode.IPHONE_FORMAT_ERROR);
				return resultData;
			}

			// 获取验证码
			String contentKey = SmsUtils.getKey(SmsType.REISTER_SMS,custMblOremail, CommonMsg.SMS_VALUE);

			// 判断验证码是否正确
			if (!verifyCode.equals(CommonCache.get(contentKey))) {
				resultData.parse(ResultCode.CODE_ERROR);
				return resultData;
			}
			CommonCache.delete(contentKey);
			resultData = custService.register(custMblOremail,MD5Utils.md5Encode(password));// 此查询需要修改
		} catch (Exception e) {
			log.error("CustController.registerAction.121row.error" + e);
			resultData.parse(ResultCode.SYSTEM_ERROR);
			return resultData;
		}

		return resultData;
	}

	@RequestMapping("shopLogin")
	public String shop_login() {
		return "shop-login";
	}

	@RequestMapping("shopLogin2")
	public String shop_login5() {
		return "shop-login2";
	}

	@RequestMapping("shop-register")
	public String shop_register() {
		return "shop-register";
	}

}
